package com.mkuspis.moviebrowser.fragments;

import android.app.Dialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mkuspis.moviebrowser.R;

import java.util.ArrayList;
import java.util.List;

public class ScreenshotsDialogFragment extends DialogFragment {

    public static final String POSITION = "position";
    public static final String TITLE = "title";
    public static final String SCREENSHOTS = "screenshots";

    private ViewPager pager;
    private ArrayList<String> screenshots;
    private int pos;
    private String movieTitle;

    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        final Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().getAttributes().windowAnimations = R.style.ScreenshotsFragmentAnimation;
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL,
                android.R.style.Theme_Translucent_NoTitleBar);

        if (savedInstanceState != null) {
            pos = savedInstanceState.getInt(POSITION, 0);
            movieTitle = savedInstanceState.getString(TITLE);
            screenshots = savedInstanceState.getStringArrayList(SCREENSHOTS);
        } else {
            final Bundle data = getArguments();
            pos = data.getInt(POSITION, 0);
            movieTitle = data.getString(TITLE);
            screenshots = data.getStringArrayList(SCREENSHOTS);
        }
    }

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             final ViewGroup container,
                             final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_screenshots, container);
    }

    @Override
    public void onViewCreated(final View view,
                              final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViewPager();
    }

    private void initViewPager() {
        pager = (ViewPager) getView().findViewById(R.id.screenshots_pager);
        final ScreenshotsPagerAdapter pagerAdapter =
                new ScreenshotsPagerAdapter(getActivity(), screenshots);
        pager.setAdapter(pagerAdapter);

        pager.setCurrentItem(pos);
    }

    @Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList(SCREENSHOTS, screenshots);
        outState.putInt(POSITION, pager.getCurrentItem());
        outState.putString(TITLE, movieTitle);
    }

    private static class ScreenshotsPagerAdapter extends PagerAdapter {

        private final Context context;
        private final List<String> screenshots;

        private ScreenshotsPagerAdapter(final Context context,
                                        final List<String> screenshots) {
            this.context = context;
            this.screenshots = screenshots;
        }

        @Override
        public int getCount() {
            return screenshots.size();
        }

        @Override
        public boolean isViewFromObject(final View view,
                                        final Object object) {
            return view.equals(object);
        }

        @Override
        public Object instantiateItem(final ViewGroup container,
                                      final int position) {
            final ImageView screenshot = new ImageView(context);
            screenshot.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            screenshot.setImageURI(Uri.parse(screenshots.get(position)));
            container.addView(screenshot);

            return screenshot;
        }

        @Override
        public void destroyItem(final View collection,
                                final int position,
                                final Object view) {
            ((ViewPager) collection).removeView((View) view);
        }
    }
}